$.fn.serializeObject = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};
function getRootPath(){
	var path = self.location + "";
	var p = path.split("/");
	return (p[3]);
}
$(function (){
    $.validationEngineLanguage.allRules.validateRepeate = {  
            "alertText": "* 已存在该内容"  
        };  
    $.validationEngineLanguage.allRules.commonLetter = {  
    		"regex": /^[0-9a-zA-Z\_\-]+$/,
            "alertText": "* 由数字、英文、</br>-、下划组成" 
        };  
	$("#baseLineForm").validationEngine({
		autoHidePrompt:true,//自动隐藏
		showOnMouseOver:false,//当鼠标移出时触发
		autoHideDelay:1500,     //自动隐藏延迟时间
		promptPosition:'bottomLeft',//提示信息的位置
		inlineValidation: false,//是否即时验证，false为提交表单时验证,默认true  
		failure : function() {
		    layer.message("验证失败，请检查");
			return false;  
		},//验证失败时调用的函数
		ajaxFormValidation: false,//开始AJAX验证
		success : function() {
		}//验证通过时调用的函数 
	});
	
	$("#cancel").click(function (){
		var index = parent.layer.getFrameIndex(window.name);
		parent.layer.close(index);
	});
	var proTypeId = $("#projectTypeId").val();
	if(proTypeId != "" && proTypeId != undefined){
		initConfigItems();
	}
	
	$("#submit").click(function(){
		var rval = $('input:radio[name="baseLineType"]:checked').val();
		if(rval == null || rval == ""){
			layer.msg("请选择基线类型");
			return false;
		}
		
		var proVal = $("#projectTypeId").val();
		if(proVal == null || proVal == ""){
			layer.msg("请选择项目类型");
			return false;
		}
		
		var val = $("input[name='baseLineItem']").val();
		if(val == null || val == ""){
			layer.msg("请至少选择一个配置项");
			return false;
		}
		
		var id = $("#ID").val();
		var baseLineName = $("#baseLineName").val();
		var baseLineIdentity = $("#baseLineIdentity").val();
		var formChecked = $('#baseLineForm').validationEngine('validate');
		if(!formChecked) return;
		var a = 0;
		var b = 0;
		$.ajax({
			url : '/'+getRootPath()+'/kcCommonController/checkNameIsExist',
			type : "post",
			dataType : "json",
			async : false,//** ！重要 同步请求。效果:ajax未进行则其他代码不运行
	  		data : {
	  			"baseLineName" : baseLineName,
	  			"ID" : id
	  			},
			success : function(json) {
				if(json.code == "1"){
					a = 1;
				}
			},
			error:function(data){
				layer.msg("网络忙，请稍后重试");
			}
		});

		$.ajax({
			url : '/'+getRootPath()+'/kcCommonController/checkNameIsExist',
			type : "post",
			dataType : "json",
			async : false,//** ！重要 同步请求。效果:ajax未进行则其他代码不运行
	  		data : {
	  			"baseLineIdentity" : baseLineIdentity,
	  			"ID" : id
	  			},
			success : function(json) {
				if(json.code == "1"){
					b = 1;
				}
			},
			error:function(data){
				layer.msg("网络忙，请稍后重试");
			}
		});
		
		if(a == 1){
			layer.msg("基线名称已存在,请重新填写");
			return false;
		}
		
		if(b == 1){
			layer.msg("基线标识已存在,请重新填写");
			return false;
		}
		
		
		var opt = {
				url : '/'+getRootPath()+'/kcCommonController/saveBaseLinenfo',
				type : "post",
			    async:false,
			   dataType: 'json',           //html(默认), xml, script, json...接受服务端返回的类型
			   success : function(json) {
					parent.refreshTable();
					layer.msg(json.message,{time:1500},function(){
						var index = parent.layer.getFrameIndex(window.name);
						parent.layer.close(index);
					});
				},
				error:function(data){
					layer.msg("网络忙，请稍后重试");
				}
			   };    
		$("#baseLineForm").ajaxSubmit(opt);
	});
	
});
/* 初始化配置项 */
function initConfigItems(){
	//项目类型ID
	var projectTypeId = $("#projectTypeId").val();
	var id = $("#ID").val();
	$.ajax({
		url : '/'+getRootPath()+'/kcCommonController/selectByProjectTypeId',
		type : "post",
		dataType : "json",
		async : false,//** ！重要 同步请求。效果:ajax未进行则其他代码不运行
  		data : {
  			"projectTypeId" : projectTypeId,
  			"baseLineId" : id
  			},
		success : function(json) {
			$("#baseLineItem").empty();
			var code = json.code;
			if("1" == code){
				var configItems = JSON.parse(json.data);
				var option = '';
				if("" != id){
					var baseItems = JSON.parse(json.message);
					if(baseItems.length > 0){
						var configs = baseItems[0].configItems;
						var flag = 0;
						for(var i in configItems){
							for(var j in configs){
								if(configs[j].ID == configItems[i].ID){
									flag = 1;
								}
							}
							if(flag == 1)option += "<li class='level1'><span class='button chk checkbox_true_full'></span><input type='hidden' name='baseLineItem' value="+configItems[i].ID+"></input><span class='configuration_item' title='配置项名称："+configItems[i].configItemName+"'>配置项名称："+configItems[i].configItemName+"</span>&nbsp;&nbsp;<span class='stage' title='阶段名称："+configItems[i].stageName+"'>阶段名称："+configItems[i].stageName+"</span></li>";
							if(flag == 0)option += "<li class='level1'><span class='button chk checkbox_false_full'></span><input type='hidden' value="+configItems[i].ID+"></input><span class='configuration_item' title='配置项名称："+configItems[i].configItemName+"'>配置项名称："+configItems[i].configItemName+"</span>&nbsp;&nbsp;<span class='stage' title='阶段名称："+configItems[i].stageName+"'>阶段名称："+configItems[i].stageName+"</span></li>";
							flag = 0;
						}
					}else{
						for(var i in configItems){
							option += "<li class='level1'><span class='button chk checkbox_false_full'></span><input type='hidden' value="+configItems[i].ID+"></input><span class='configuration_item' title='配置项名称："+configItems[i].configItemName+"'>配置项名称："+configItems[i].configItemName+"</span>&nbsp;&nbsp;<span class='stage' title='阶段名称："+configItems[i].stageName+"'>阶段名称："+configItems[i].stageName+"</span></li>";
						}	
					}
				}else{
					for(var i in configItems){
						option += "<li class='level1'><span class='button chk checkbox_false_full'></span><input type='hidden' value="+configItems[i].ID+"></input><span class='configuration_item' title='配置项名称："+configItems[i].configItemName+"'>配置项名称："+configItems[i].configItemName+"</span>&nbsp;&nbsp;<span class='stage' title='阶段名称："+configItems[i].stageName+"'>阶段名称："+configItems[i].stageName+"</span></li>";
					}
				}
				//$("#baseLineItem option").remove();
				$("#baseLineItem").append(option);
				bindFunction();
			}
			if("0" == code || "2" == code){layer.msg(json.message);}
			
		},
		error:function(data){
			layer.msg("网络忙，请稍后重试");
		}
	});

}
function bindFunction(){
	$("ul li").each(function(i,ele){
		$(this).find("span:eq(0)").mouseover(function(){
			if($(this).hasClass("checkbox_true_full")){
				$(this).removeClass("checkbox_true_full");
		    	$(this).addClass("checkbox_true_full_focus");
			}
			if($(this).hasClass("checkbox_false_full")){
				$(this).removeClass("checkbox_false_full");
				$(this).addClass("checkbox_false_full_focus");
			}
		});
		$(this).find("span:eq(0)").mouseleave(function(){
			if($(this).hasClass("checkbox_true_full_focus")){
				$(this).removeClass("checkbox_true_full_focus");
		    	$(this).addClass("checkbox_true_full");
			}
			if($(this).hasClass("checkbox_false_full_focus")){
				$(this).removeClass("checkbox_false_full_focus");
				$(this).addClass("checkbox_false_full");
			}
		});
		$(this).find("span:eq(0)").click(function(){
			if($(this).hasClass("checkbox_true_full_focus")){
				$(this).removeClass("checkbox_true_full_focus");
				$(this).addClass("checkbox_false_full_focus");
				$(this).parents("li").find("input[type='hidden']").removeAttr("name");
			}else if($(this).hasClass("checkbox_false_full_focus")){
				$(this).removeClass("checkbox_false_full_focus");
				$(this).addClass("checkbox_true_full_focus");
				$(this).parents("li").find("input[type='hidden']").attr("name","baseLineItem");
			}
		});
	});
}
